/*
 * Copyright (c) 2006, 2021, webrx.cn All rights reserved.
 *
 */
package cn.webrx;

import java.util.Arrays;
import java.util.Comparator;
import java.util.Random;

/**
 * <p>Project: javase - StringSort
 * <p>Powered by webrx On 2021-12-21 14:29:10
 * <p>Created by IntelliJ IDEA
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class StringSort {
    public static void main(String[] args) {
        String[] langs = {"java", "javascript", "c", "go", "python"};
        System.out.println(Arrays.toString(langs));

        //升序
        Arrays.sort(langs);
        System.out.println(Arrays.toString(langs));

        //降序
        //Arrays.sort(langs,(a,b)->b.compareTo(a));
        Arrays.sort(langs, Comparator.reverseOrder());
        System.out.println(Arrays.toString(langs));

        //乱序
        Random rand = new Random();
        Arrays.sort(langs, (a, b) -> rand.nextBoolean() ? 1 : -1);
        Arrays.sort(langs, (a, b) -> rand.nextBoolean() ? 1 : -1);
        Arrays.sort(langs, (a, b) -> rand.nextBoolean() ? 1 : -1);
        Arrays.sort(langs, (a, b) -> rand.nextBoolean() ? 1 : -1);
        System.out.println(Arrays.toString(langs));

        //根据字母个数
        //Arrays.sort(langs, (a, b) -> a.length() - b.length());
        Arrays.sort(langs, Comparator.comparingInt(String::length));
        System.out.println(Arrays.toString(langs));

        //根据字母个数
        Arrays.sort(langs, (a, b) -> b.length() - a.length());
        System.out.println(Arrays.toString(langs));

        //乱序
        Arrays.sort(langs, (a, b) -> Math.random() > .5 ? 1 : -1);
        Arrays.sort(langs, (a, b) -> Math.random() > .5 ? 1 : -1);
        Arrays.sort(langs, (a, b) -> Math.random() > .5 ? 1 : -1);
        Arrays.sort(langs, (a, b) -> Math.random() > .5 ? 1 : -1);
        System.out.println(Arrays.toString(langs));
    }
}
